<template>
    <div>
        <div style="margin-left: 50px;margin-top: 20px;margin-bottom: 50px;">
            账户名：{{ hasGetUserInfo.username }}
        </div>
        <div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="原密码：" prop="oldPass">
                    <el-input v-model.number="ruleForm.oldPass"></el-input>
                </el-form-item>
                <el-form-item label="新密码：" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码：" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import userApi from "@/api/user/user";
import store from '../../../store'
export default {
    data() {
        var checkOldPassword = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('原始密码不能为空'));
            }else{
                callback()
            }
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
            callback(new Error('请输入密码'));
            } else {
            if (this.ruleForm.checkPass !== '') {
                this.$refs.ruleForm.validateField('checkPass');
            }
            callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
            callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
            callback(new Error('两次输入密码不一致!'));
            } else {
            callback();
            }
        }
        return {
            hasGetUserInfo: '',
            ruleForm: {
                id: '',
                pass: '',
                checkPass: '',
                oldPass: ''
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                oldPass: [
                    { validator: checkOldPassword, trigger: 'blur' }
                ]
            }

        }
    },
    created() {
        this.fetchData()
        this.hasGetUserInfo = store.getters.name
    },
    methods: {
        fetchData(){
            
        },
        submitForm(formName) {
            this.ruleForm.id = this.hasGetUserInfo.id
            this.$refs[formName].validate((valid) => {
            if (valid) {
                userApi.updatePasswordById(this.ruleForm)
                .then(response =>{
                    this.$message({
                    message: '修改密码成功',
                    type: 'success'
                    });
                    this.$refs[formName].resetFields();
                    this.$store.dispatch('user/logout')
                    this.$router.push(`/login?redirect=${this.$route.fullPath}`)
                })
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
        
    },
}
</script>

<style>
#app{
    background: rgb(246, 246, 246);
    height: 100%;
    overflow: scroll;
  }
</style>